package br.odb.bzk3.android;

import java.util.ArrayList;
import java.util.Vector;

import android.util.Log;
import br.odb.libscene.Door;
import br.odb.utils.math.Vec3;

/**
 * 
 * @author monty
 *
 */
public class GameDoor extends Door {
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	private static PositionalMediaPlayer closeDoorSound;
	/**
	 * 
	 */
	private static PositionalMediaPlayer openDoorSound;
	
	private GameDoor master;
	private ArrayList< GameDoor > sons = new ArrayList< GameDoor >();
	// ------------------------------------------------------------------------------------------------------------
	
	public void setMaster( GameDoor door) {
		master = door;
		
		if ( getMesh() == null )
			setMesh( master.getMesh() );
	}
	

	public void addSon( GameDoor door ) {
		sons.add( door );
		door.setMesh( getMesh() );
	}
	/**
	 * 
	 * @param sector
	 * @param pos
	 */
	public GameDoor(int sector, Vec3 pos) {
		super(sector);
		
		openDoorSound = PositionalMediaPlayer.getFor(pos, R.raw.dooropen);
		AudioManager.getInstance().registerPlayer(openDoorSound);
		closeDoorSound = PositionalMediaPlayer.getFor(pos, R.raw.doorclose);
		AudioManager.getInstance().registerPlayer(closeDoorSound);
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void close() {
	//	super.close();
		
		if ( master != null )
			master.close();
		else if ( sons != null )
				for ( int c = 0; c < sons.size(); ++c )
					sons.get( c ).justClose();
		
		closeDoorSound.playUnique();
	}
	
	public void justOpen() {
		super.open();
	}
	
	public void justClose() {
		super.close();
	}	
	// ------------------------------------------------------------------------------------------------------------
	/***
	 * 
	 */
	@Override
	public void open() {

		
		if ( master != null ) {
			Log.d( "BZK3", "Asking my master to open it!" );
			master.open();
		} else if ( sons != null )
				for ( int c = 0; c < sons.size(); ++c ) {
					Log.d( "BZK3", "opening for my child " + c );
					sons.get( c ).justOpen();		
				}
		else {
			super.open();
		}
		
		openDoorSound.playUnique();
	}
	// ------------------------------------------------------------------------------------------------------------
}
